package com.common.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取当前用户使用的角色
 */
public class CurrentRoleUtils {

    /**
     * 获取当前 HttpServletRequest
     * @return HttpServletRequest 对象
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return attributes != null ? attributes.getRequest() : null;
    }


    /**
     * 获取当前登陆用户的使用角色的key值
     * @param
     * @return
     */
    public static String getCurrentRoleKey() {
        HttpServletRequest request = getRequest();
        return (request != null) ? request.getHeader("X-RoleKey"): null;
    }
}
